
import { _decorator, Component, Node, Prefab, Color } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
import GlobalPool, { customPoolScript } from '../../Script/Common/GlobalPool';
import { RankItem } from './RankItem';
import GlobalData from '../../Script/Common/GlobalData';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
import { EventType } from '../../Script/GameSpecial/GameEventType';
const { ccclass, property } = _decorator;

@ccclass('RankLayer')
export class RankLayer extends yyComponent {

    public init() {
        this.initRank();
        this.onEvents();
    }
    public reset() {
        this.resetRank();
    }
    protected onEvents() {
        this.on(EventType.LevelEvent.killOneRole, this.onKillOneRole, this);
    }
    protected setData() {
        this.addPlayerRank();
        this.addEnemyRank();
    }

    public show() {
        this.node.active = true;
        this.reset();
        this.setData();
        this.onEvents();
    }

    //排名数据
    protected rankData: { roleId: number, score: number, rankItem: RankItem }[] = [];
    @property(Prefab)
    protected rankItemPrefab: Prefab = null;
    @property(Node)
    protected rankLayer: Node = null;
    protected initRank() {
        GlobalPool.createPool(this.rankItemPrefab.data.name, this.rankItemPrefab, "RankItem");
        this.rankData = [];
    }
    protected resetRank() {
        GlobalPool.putAllChildren(this.rankLayer, true);
        this.rankData = [];
    }
    protected addPlayerRank() {
        let player = GlobalData.get(GlobalEnum.myGlobalDataType.player);
        let data = {
            rank: 1,
            score: 0,
            name: "你",
        };
        let item = this.addRankItem(data);
        this.rankData.push({
            roleId: player.Id,
            score: 0,
            rankItem: item,
        });
        item.setColor(Color.YELLOW);
    }
    protected addEnemyRank() {
        let enemies = GlobalData.get(GlobalEnum.myGlobalDataType.enemies);
        let names = GlobalData.get(GlobalEnum.myGlobalDataType.matchEnemyNames);
        for (let i = 0, c = enemies.length; i < c; ++i) {
            let data = {
                rank: i + 2,
                score: 0,
                name: names[i],
            };
            let item = this.addRankItem(data);
            this.rankData.push({
                roleId: enemies[i].Id,
                score: 0,
                rankItem: item,
            });
            item.setColor(Color.WHITE);
        }
    }
    protected addRankItem(data: { rank: number, score: number, name: string }) {
        let node = GlobalPool.get(this.rankItemPrefab.data.name, data);
        this.rankLayer.addChild(node);
        return node[customPoolScript];
    }

    protected onKillOneRole(atkRole: yyComponent) {
        let id = atkRole.Id;
        for (let i = this.rankData.length - 1; i >= 0; --i) {
            if (this.rankData[i].roleId == id) {
                this.rankData[i].score += 1;
                break;
            }
        }
        this.rankData.sort((a, b) => {
            if (a.score == b.score) {
                return a.rankItem.Id < b.rankItem.Id ? 1 : -1;
            }
            return a.score < b.score ? 1 : -1;
        });
        for (let i = this.rankData.length - 1; i >= 0; --i) {
            this.rankData[i].rankItem.changeRank({ rank: i + 1, score: this.rankData[i].score });
        }
    }

}

